package com.clusterswisdom.horn.api.ioc.struct;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Element;

import com.clusterswisdom.horn.api.db.StructMeger;

public class StructDest {

	private String table;
	
	private String pkField;
	
	private StructMeger meger;
	
	private List<DestField> fields;
	
	public StructDest(Element element) {
		this.table = element.attributeValue("table");
		this.pkField = element.attributeValue("pkField");
		
		String megerValue = element.attributeValue("meger");
		if (megerValue != null) {
			meger = instanceMeger(megerValue);
		}
		
		fields = new ArrayList<DestField>(10);
		Iterator<Element> elementIterator = element.elementIterator("field");
		while (elementIterator.hasNext()) {
			Element fieldElement = elementIterator.next();
			fields.add(new DestField(fieldElement));
		}
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getPkField() {
		return pkField;
	}

	public void setPkField(String pkField) {
		this.pkField = pkField;
	}

	public StructMeger getMeger() {
		return meger;
	}

	public List<DestField> getFields() {
		return fields;
	}

	public void setFields(List<DestField> fields) {
		this.fields = fields;
	}
	
	private StructMeger instanceMeger(String merger) {
		try {
			return (StructMeger)Thread.currentThread().getContextClassLoader().loadClass(merger).newInstance();
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
			
		}
	}
}